package com.leanit.pmc.center.user;

import com.leanit.pmc.common.dao.CommonDao;
import com.leanit.pmc.reg.bean.SysUser;
import com.sdeport.security.shiro.SDUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 用户信息管理Controller
 */
@Controller
@RequestMapping("center")
@Scope("prototype")
public class CenterController {

    private final org.slf4j.Logger logger = LoggerFactory.getLogger(CenterController.class);


    @Autowired
    private CenterSerivce centerSerivce;

    @Autowired
    private CommonDao commonDao;

    @Value("${shiro.login.url}")
    private String loginUrl;

    /**
     * 跳转密码修改页面
     *
     * @param status 1:修改成功；0:修改失败
     * @param map
     * @return
     */
    @RequestMapping(value = "passwordEdit", method = RequestMethod.GET)
    public String passwordEdit(String status, ModelMap map) {
        map.addAttribute("status", status);
        return "center/operator/password_edit";
    }

    /**
     * 跳转用户信息页面
     *
     * @param status
     * @param request
     * @param map
     * @return
     */
    @RequestMapping(value = "operateInfo", method = RequestMethod.GET)
    public String operateInfo(String status, HttpServletRequest request, ModelMap map) {
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute("UserInfo");
        SysUser qryUser = new SysUser();
        qryUser.setUsername(userInfo.getLogName());
        SysUser user = commonDao.qryOne("sys_user", qryUser, " id desc");
        if (null == user) {
            userInfo.setName("");
            userInfo.setPhone("");
        } else {
            userInfo.setName(user.getCnName());
            userInfo.setPhone(user.getMobilePhoneNumber());
        }
        map.addAttribute("userInfo", userInfo);
        map.addAttribute("idNo", centerSerivce.getIdNo(userInfo.getLogName()));
        map.addAttribute("status", status);
        return "center/operator/operate_info";
    }

    /**
     * 验证原密码正确性
     *
     * @param password
     * @return
     */
    @RequestMapping(value = "checkOldPassword", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public boolean checkOldPassword(String password) {
        return "12".equals(password);
    }

    /**
     * 更新密码
     *
     * @param request
     * @param map
     * @return
     */
    @RequestMapping(value = "updatePassword", method = {RequestMethod.GET, RequestMethod.POST})
    public String updatePassword(HttpServletRequest request, ModelMap map) {
        String password_old = request.getParameter("password_old");
        String password_new = request.getParameter("password_new");
        logger.info("password_old=" + password_old);
        logger.info("password_new=" + password_new);
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute("UserInfo");
        if (SDUtils.updatePassword(userInfo.getLogName(), password_old, password_new)) {
            map.addAttribute("status", "1");
            map.addAttribute("loginUrl", loginUrl);
        } else {
            map.addAttribute("status", "0");
        }
        return "center/operator/password_edit";
    }

    /**
     * 更新用户信息
     *
     * @param response
     * @param logName
     * @param idNo
     * @param mobilePhoneNumber
     */
    @RequestMapping(value = "updateOperateInfo", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateOperateInfo(HttpServletResponse response, String logName, String cnName, String idNo,
                                  String mobilePhoneNumber, HttpSession session) {
        try {
            centerSerivce.updateOperateInfo(logName, cnName, idNo, mobilePhoneNumber);
            UserInfo userInfo = (UserInfo) session.getAttribute("UserInfo");
            userInfo.setName(cnName);
            userInfo.setPhone(mobilePhoneNumber);
            response.sendRedirect("operateInfo?status=1");
        } catch (Exception e) {
            try {
                response.sendRedirect("operateInfo?status=0");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
    }

}
